<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>

<body>
    <script>
        function promiseAjax(url) {
            var promise = new Promise((resolve, reject) => {
                $.ajax({
                    type: 'get',
                    url: url,
                    success: function(data) {
                        resolve(data);
                    },
                    error: function(err) {
                        reject(err);
                    }
                });
            });
            return promise;
        };
        var baseURl = "http://localhost:3000";
        // 三个异步请求
        var ajax1 = promiseAjax(baseURl + '/api/promisedata1');
        var ajax2 = promiseAjax(baseURl + '/api/promisedata2');
        var ajax3 = promiseAjax(baseURl + '/api/promisedata3');
        // Promise.all([]):参数是数组,返回结果是promise对象一起执行得到的结果,也是一个promise对象
        Promise.all([ajax1, ajax2, ajax3]).then((data) => {
            console.log(data);
        });
        // `Promise.race`方法同样接受一个数组作参数。并把第一个改变状态的promise的返回值,也是一个promise对象
        Promise.race([ajax1, ajax2, ajax3]).then((data) => {
            console.log(data);
        });
    </script>
</body>

</html>